package com.sgb.kjwl.view.ui.activity.self.carMaster;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.util.TypedValue;
import android.view.View;

import androidx.core.content.ContextCompat;
import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.sgb.kjwl.AppActivity;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.ActivityOwnPathBinding;
import com.sgb.kjwl.model.entity.self.OwnPathEntity;
import com.sgb.kjwl.view.ui.adapter.self.carMasters.OwnPathsAdapter;
import com.swgk.core.dialog.RoundDialog;
import com.sgb.kjwl.viewmodel.self.carMakers.OwnPathsModel;
import com.swgk.core.util.MToast;
import com.swgk.core.xrecyclerview.XRecyclerView;

import java.util.ArrayList;
import java.util.List;

/**
 * Describe:    我的路线-列表（货源订阅）
 * Author:  yangLei
 * Date:    2021/6/18
 */
public class OwnPathsActivity extends AppActivity implements OwnPathsAdapter.OwnPathItemClickListener {

    private ActivityOwnPathBinding mBinding;
    private OwnPathsModel viewModel;

    private OwnPathsAdapter pathsAdapter;
    private List<OwnPathEntity> ownPaths = new ArrayList<>();

    public static void start(Context context) {
        Intent intent = new Intent(context, OwnPathsActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        context.startActivity(intent);
    }


    @Override
    protected void initView() {
        viewModel = new ViewModelProvider(this).get(OwnPathsModel.class);
        mBinding = DataBindingUtil.setContentView(this, R.layout.activity_own_path);
        mBinding.include.titleView.setText("我的线路");
        mBinding.include.titleView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 17);
        mBinding.include.rightView.setText("新增线路");
        mBinding.include.rightView.setVisibility(View.VISIBLE);
        mBinding.include.rightView.setOnClickListener(this);
        mBinding.include.ivBack.setOnClickListener(v -> finish());

        if (pathsAdapter == null)
            pathsAdapter = new OwnPathsAdapter(this, null);
        pathsAdapter.setClickListener(this);
        DividerItemDecoration divider = new DividerItemDecoration(this, DividerItemDecoration.VERTICAL);
        divider.setDrawable(ContextCompat.getDrawable(this, R.drawable.shape_list_divide));
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        mBinding.recyclerView.addItemDecoration(divider);
        mBinding.recyclerView.setLayoutManager(layoutManager);
        mBinding.recyclerView.setAdapter(pathsAdapter);
        mBinding.recyclerView.setLoadingMoreEnabled(false);
        mBinding.recyclerView.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                viewModel.getMySubPath(OwnPathsActivity.this);
            }

            @Override
            public void onLoadMore() {

            }
        });

        viewModel.pathEntities.observe(this, ownPathEntities -> loadingData(ownPathEntities));
        viewModel.deleteLD.observe(this, aBoolean -> viewModel.getMySubPath(OwnPathsActivity.this));
    }

    @Override
    protected void onResume() {
        super.onResume();
        viewModel.getMySubPath(this);
    }

    /**
     * 加载数据
     */
    private void loadingData(List<OwnPathEntity> ownPathEntities) {
        mBinding.recyclerView.refreshComplete();
        if (ownPathEntities == null || ownPathEntities.size() <= 0) {
            mBinding.emptyView.setVisibility(View.VISIBLE);
            mBinding.recyclerView.setVisibility(View.GONE);
        } else if (ownPathEntities.size() > 0) {
            ownPaths.clear();
            ownPaths.addAll(ownPathEntities);
            pathsAdapter.setDatas(ownPaths);
            mBinding.emptyView.setVisibility(View.GONE);
            mBinding.recyclerView.setVisibility(View.VISIBLE);
        }
    }


    @Override
    public void onMultiClick(View v) {
        switch (v.getId()) {
            case R.id.right_view:
                if (ownPaths.size() >= 10) {
                    MToast.showToast(this, "最多支持设置10条线路");
                    return;
                }
                //新增路线
                EditSubscribePathActivity.start(this);
                break;
        }
    }

    @Override
    public void onEdit(OwnPathEntity pathEntity) {
        //编辑路线
        EditSubscribePathActivity.start(this, true, pathEntity);
    }

    /**
     * 删除路线
     *
     * @param pathEntity
     */
    @Override
    public void onDelete(OwnPathEntity pathEntity) {
        new RoundDialog.Builder(getActivity())
                .title("删除确认")
                .content("删除后将不再推送该线路货源，您确定要删除吗？")
                .cancel("取消")
                .confirm("确定")
                .confirmCallback(v -> {
                    if (!TextUtils.isEmpty(pathEntity.getId())) {
                        viewModel.deletePath(this, pathEntity.getId());
                    }
                })
                .show();
    }
}
